package com.yf.contactprovider;

import java.util.ArrayList;

import android.app.Activity;
import android.content.ContentResolver;
import android.content.ContentValues;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;

public class MainActivity extends Activity {

	private EditText et_name;
	private EditText et_phone;
	private EditText et_email;
	private EditText et_address;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		et_name = (EditText) findViewById(R.id.et_name);
		et_phone = (EditText) findViewById(R.id.et_phone);
		et_email = (EditText) findViewById(R.id.et_email);
		et_address = (EditText) findViewById(R.id.et_address);
	}

	public void queryContacts(View v) {
		ContentResolver resolver = getContentResolver();
		Uri data_uri = Uri.parse("content://com.android.contacts/data");
		Uri raw_contacts_uri = Uri.parse("content://com.android.contacts/raw_contacts");
		Cursor cursor = resolver.query(raw_contacts_uri, new String[] { "contact_id" }, null, null, null);
		ArrayList<Contact> contacts = new ArrayList<Contact>();
		while (cursor.moveToNext()) {
			String contact_id = cursor.getString(0);
			System.out.println(contact_id);
			String[] projection = { "data1", "mimetype" };
			String selection = "raw_contact_id=?";
			String[] selectionArgs = { contact_id };
			Cursor cursor2 = resolver.query(data_uri, projection, selection, selectionArgs, null);
			Contact contact = new Contact();
			while (cursor2.moveToNext()) {
				String data1 = cursor2.getString(0);
				String type = cursor2.getString(1);
				if ("vnd.android.cursor.item/phone_v2".equals(type)) {
					contact.phone = data1;
				} else if ("vnd.android.cursor.item/postal-address_v2".equals(type)) {
					contact.address = data1;
				} else if ("vnd.android.cursor.item/email_v2".equals(type)) {
					contact.email = data1;
				} else if ("vnd.android.cursor.item/name".equals(type)) {
					contact.name = data1;
				}
			}
			contacts.add(contact);
		}
		System.out.println(contacts);
	}

	public void insertContacts(View v) {
		ContentResolver resolver = getContentResolver();
		
		Uri raw_contacts_uri = Uri.parse("content://com.android.contacts/raw_contacts");
		Cursor cursor = resolver.query(raw_contacts_uri, new String[] { "contact_id" }, null, null, null);
		int count = cursor.getCount();
		cursor.close();
		ContentValues values = new ContentValues();
		values.put("contact_id", count + 1);
		resolver.insert(raw_contacts_uri, values);
		
		Uri data_url = Uri.parse("content://com.android.contacts/data");
		ContentValues v2 = new ContentValues();
		v2.put("raw_contact_id", count + 1);
		v2.put("mimetype", "vnd.android.cursor.item/phone_v2");
		v2.put("data1", et_phone.getText().toString().trim());
		System.out.println(v2);
		resolver.insert(data_url, v2);
		
		ContentValues v3 = new ContentValues();
		v3.put("raw_contact_id", count + 1);
		v3.put("mimetype", "vnd.android.cursor.item/postal-address_v2");
		v3.put("data1", et_address.getText().toString().trim());
		System.out.println(v3);
		resolver.insert(data_url, v3);
		
		ContentValues v4 = new ContentValues();
		v4.put("raw_contact_id", count + 1);
		v4.put("mimetype", "vnd.android.cursor.item/email_v2");
		v4.put("data1", et_email.getText().toString().trim());
		System.out.println(v4);
		resolver.insert(data_url, v4);
		
		ContentValues v5 = new ContentValues();
		v5.put("raw_contact_id", count + 1);
		v5.put("mimetype", "vnd.android.cursor.item/name");
		v5.put("data1", et_name.getText().toString().trim());
		System.out.println(v5);
		resolver.insert(data_url, v5);
	}
}
